//
//  MODingCanViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/10/17.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MODingCanViewController.h"
#import "MODingCanViewCell.h"
#import "MOCanTypeModel.h"
#import "MOFoodDetailView.h"

@interface MODingCanViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UIView *headerView;
//我的余额
@property(nonatomic,strong)UIButton *myMoneyButton;
//餐厅选择
@property(nonatomic,strong)UIButton *selectedButton;
//餐厅id
@property(nonatomic,strong)NSString *restaurantId;
//当前选择的时间
@property(nonatomic,strong)NSString *selectedDate;
//时间选择
@property(nonatomic,strong)UIButton *dateButton;
//选择的订餐类型
@property(nonatomic,strong)NSString *canType;
//总价
@property(nonatomic,strong)UILabel *allPriceLabel;
//余额
@property(nonatomic,strong)NSString *myMoney;
//菜谱id
@property(nonatomic,strong)NSString *recipeId;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)UITableView *leftTableView;

@property(nonatomic,strong)UITableView *rightTableView;

@property(nonatomic,strong)NSMutableArray *canTingArray;

@property(nonatomic,strong)NSMutableArray *leftDataArray;

@property(nonatomic,strong)NSMutableArray *rightDataArray;

@end

@implementation MODingCanViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
}

-(void)creatView
{
    self.title = @"订餐";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.leftTableView];
    
    [self.view addSubview:self.rightTableView];
    
    [self.view addSubview:self.bottomView];
}

-(void)initData
{
    [self getMyPrice];
    
    //餐厅
    NSString *postUrl1 = [NSString stringWithFormat:@"%@?&authorization=%@&userId=%@",GETRESTAURANTLIST,[NSString URLEncodedString:[MOUser sharedMOUser].token],[MOUser sharedMOUser].userName];
    [PPNetworkHelper POST:postUrl1 parameters:nil success:^(id responseObject) {
        NSLog(@"餐厅：%@",responseObject);
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            self.canTingArray = responseObject;
            if (self.canTingArray.count>0) {
                [self.selectedButton setTitle:self.canTingArray[0][@"restaurantName"] forState:UIControlStateNormal];
                [self.selectedButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
                self.restaurantId = self.canTingArray[0][@"restaurantId"];
                
                //获取订餐类型
                [self getCanTypeData];
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"餐厅：%@",[error localizedDescription]);
    }];
    
}

-(void)getMyPrice{
    //余额
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",USERACCOUNT,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"userId":[MOUser sharedMOUser].userName
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"余额：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([responseObject isKindOfClass:[NSString class]]) {
            NSString *m = [self removeFloatAllZero:responseObject];
            [self.myMoneyButton setTitle:[NSString stringWithFormat:@"¥ %@",m] forState:UIControlStateNormal];
            self.myMoney = m;
        }
        
    } failure:^(NSError *error) {
        NSLog(@"余额：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = string;
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

//获取订餐类型
-(void)getCanTypeData
{
    if (self.restaurantId.length==0) {
        return;
    }
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",CHECKCANDO,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"restaurantId":self.restaurantId,
                          @"dateTime":self.selectedDate,
                          @"type":@"order"
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"订餐类型：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dataDic = responseObject;
            
            self.leftDataArray = [NSMutableArray array];
            NSArray *titleArr = @[@"早餐",@"午餐",@"晚餐",@"其他"];
            for (int i = 0; i<titleArr.count; i++) {
                NSArray *arr = dataDic.allKeys;
                NSInteger have = 0;
                NSString *recipeId = @"";
                for (int j = 0; j<arr.count; j++) {
                    if ([[arr[j] substringToIndex:1] integerValue]==i+1&&[dataDic[arr[j]] integerValue]==1) {
                        have=1;
                        recipeId = [arr[j] substringFromIndex:2];
                    }
                }
                if (have==1) {
                    MOCanTypeModel *model = [[MOCanTypeModel alloc]init];
                    model.nameStr = titleArr[i];
                    model.idStr = [NSString stringWithFormat:@"%d",i+1];
                    model.recipeId = recipeId;
                    [self.leftDataArray addObject:model];
                }
            }
            
            if (self.leftDataArray.count>0) {
                MOCanTypeModel *firstModel = self.leftDataArray[0];
                self.canType = firstModel.idStr;
                self.recipeId = firstModel.recipeId;
                
                [self getFoodData];
            }
            [self.leftTableView reloadData];
            UITableViewCell *cell = [self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell.selected = YES;
        }
        
    } failure:^(NSError *error) {
        NSLog(@"订餐类型：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)getFoodData
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",GETDISHLIST,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"restaurantId":self.restaurantId,
                          @"recipeTime":self.selectedDate,
                          @"mealTimeCategory":self.canType
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"餐品：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray *arr = responseObject;
            if (arr.count==0) {
                [[ADShowMessage shareShowMessage]showTextOnly:@"该时间菜品未录入，请稍后再试" showInView:self.view];
            }else{
                self.rightDataArray = [NSMutableArray array];
                for (int i = 0; i<arr.count; i++) {
                    MODingCanModel *model = [[MODingCanModel alloc]init];
                    model.nameStr = arr[i][@"dishName"];
                    model.monthPayNumber = arr[i][@"monthSales"];
                    model.priceStr = arr[i][@"price"];
                    model.idStr = arr[i][@"dishId"];
                    model.maxNumber = arr[i][@"maxNumber"];
                    model.remark = arr[i][@"mark"];
                    model.cook = arr[i][@"cook"];
                    model.numberStr = @"0";
                    model.material = arr[i][@"material"];
                    [self.rightDataArray addObject:model];
                }
                [self.rightTableView reloadData];
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"餐品：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

#pragma mark -- 选择餐厅
-(void)clickSelectedButton
{
    SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    NSMutableArray *muArr = [NSMutableArray array];
    for (int i = 0; i<self.canTingArray.count; i++) {
        [muArr addObject:self.canTingArray[i][@"restaurantName"]];
    }
    pickview.numberArray = [NSArray arrayWithArray:muArr];
    __weak typeof (self)weakSelf = self;
    pickview.clickIndexBlock = ^(NSInteger index) {
        [weakSelf.selectedButton setTitle:weakSelf.canTingArray[0][@"restaurantName"] forState:UIControlStateNormal];
        [weakSelf.selectedButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        weakSelf.restaurantId = weakSelf.canTingArray[0][@"restaurantId"];
        
        [weakSelf getCanTypeData];
    };
    [WINDOW addSubview:pickview];
}

#pragma mark -- 选择时间
-(void)dateButtonClick
{
    XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
    pickerDate.maxComponentCount = 3;
    pickerDate.xlsn0wDelegate = self;
    [pickerDate show];
}

- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    NSString *date = [NSString stringWithFormat:@"%ld-%02ld-%02ld",(long)year,(long)month,(long)day];
    
    if ([[date stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]< [[NSString getCurrentTimesAndFormat:@"YYYYMMdd"] integerValue]) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"选择的时间不能小于当前时间" showInView:self.view];
        return;
    }
    
    self.selectedDate = date;
    [self.dateButton setTitle:self.selectedDate forState:UIControlStateNormal];
    
    [self getCanTypeData];
}

-(void)countPrice{
    
    CGFloat allPrice = 0.0;
    for (int i = 0; i<self.rightDataArray.count; i++) {
        MODingCanModel *model = self.rightDataArray[i];
        if ([model.numberStr integerValue]>0) {
            allPrice += [model.priceStr floatValue];
        }
    }
    
    NSString *m = [self removeFloatAllZero:[NSString stringWithFormat:@"%f",allPrice]];
    
    self.allPriceLabel.attributedText = [self contentString:[NSString stringWithFormat:@"总额：%@元",m] valueStr:[NSString stringWithFormat:@"%@",m]];
    
}

#pragma mark -- 提交
-(void)clickSubMitButton
{
    NSString *orderId = [NSString getNowTimeTimestamp];
    NSInteger count = 0;
    NSInteger totalNumber = 0;
    double allPrice = 0.0;
    NSMutableArray *muArr = [NSMutableArray array];
    for (int i = 0; i<self.rightDataArray.count; i++) {
        MODingCanModel *model = self.rightDataArray[i];
        if ([model.numberStr integerValue]>0) {
            count++;
            totalNumber+=[model.numberStr integerValue];
            allPrice += [model.priceStr doubleValue]*[model.numberStr integerValue];
            
            NSDictionary *dic = @{
                                  @"servings":@([model.numberStr integerValue]),
                                  @"orderId":orderId,
                                  @"dishId":model.idStr
                                  };
            [muArr addObject:dic];
        }
    }
    if (count==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"你还未选餐!" showInView:self.view];
        return;
    }
    
    if ([self.myMoney doubleValue]< allPrice) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"余额不足!" showInView:self.view];
        return;
    }
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",CHECKHASDEAL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"recipeId":self.recipeId,
                          @"createUser":[MOUser sharedMOUser].userName,
                          @"mealTimeCategory":self.canType,
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"判定菜谱中用餐类型是否已有订单：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([responseObject[@"flag"] integerValue]==1) {
                [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
            }else{
                [self submitAndCreatOrderAndDetails:muArr orderId:orderId allPrice:allPrice totalNumber:totalNumber];
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"判定菜谱中用餐类型是否已有订单：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

-(void)submitAndCreatOrderAndDetails:(NSMutableArray *)details orderId:(NSString *)orderId allPrice:(double)allPrice totalNumber:(NSInteger)totalNumber
{
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?&authorization=%@",ADDORDER,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSString *m = [self removeFloatAllZero:[NSString stringWithFormat:@"%f",allPrice]];
    NSDictionary *dic = @{
                          @"recipeId":self.recipeId,
                          @"createUser":[MOUser sharedMOUser].userName,
                          @"mealTimeCategory":self.canType,
                          @"orderId":orderId,
                          @"amount":m,
                          @"totalNumber":@(totalNumber),
                          @"details":details
                          };
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSDictionary *endDic = @{
                             @"order":jsonStr
                             };
    NSLog(@"%@\n%@",dic,endDic);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"订餐：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {

            [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
            if ([responseObject[@"flag"] integerValue]==1) {
                [self getMyPrice];
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"订餐：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag==100) {
        return 1;
    }else{
        return _rightDataArray.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==100) {
        return _leftDataArray.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==100) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            MOCanTypeModel *model = self.leftDataArray[indexPath.row];
            cell.textLabel.text = model.nameStr;
            
            cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
            cell.selectedBackgroundView.backgroundColor = KBlueColor_B7DAFF;
            
        }
        return cell;
    }else{
        MODingCanViewCell *cell = [MODingCanViewCell cellWithTableView:tableView andIndexPath:indexPath cellWidth:self.rightTableView.width];
        MODingCanModel *model = self.rightDataArray[indexPath.section];
        cell.model = model;
        __weak typeof (MODingCanModel *)weakModel = model;
        __weak typeof (self)weakSelf = self;
        cell.clickAddBlock = ^{
            NSInteger number = [weakModel.numberStr integerValue];
            if (number<[weakModel.maxNumber integerValue]) {
                number++;
                weakModel.numberStr = [NSString stringWithFormat:@"%ld",(long)number];
                [weakSelf.rightDataArray replaceObjectAtIndex:indexPath.section withObject:weakModel];
                [weakSelf.rightTableView reloadData];
                
                [weakSelf countPrice];
            }else{
                [[ADShowMessage shareShowMessage]showTextOnly:[NSString stringWithFormat:@"该菜品最大购买数为%@",weakModel.maxNumber] showInView:WINDOW];
            }
        };
        cell.clickReduceBlock = ^{
            NSInteger number = [weakModel.numberStr integerValue];
            if (number>0) {
                number--;
                weakModel.numberStr = [NSString stringWithFormat:@"%ld",(long)number];
                [weakSelf.rightDataArray replaceObjectAtIndex:indexPath.section withObject:weakModel];
                [weakSelf.rightTableView reloadData];
                
                [weakSelf countPrice];
            }
        };
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==100) {
        return 60;
    }else{
        return 80;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==200) {
        MODingCanModel *model = self.rightDataArray[indexPath.section];
        MOFoodDetailView *view = [[MOFoodDetailView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        MOFoodDetailModel *detailModel = [[MOFoodDetailModel alloc]init];
        detailModel.foodNameStr = model.nameStr;
        detailModel.price = [NSString passNullForString:model.priceStr];
        detailModel.cook = [NSString passNullForString:model.cook];
        detailModel.yuanLiao = [NSString passNullForString:model.material];
        detailModel.mark = [NSString passNullForString:@""];
        
        view.model = detailModel;
        [self.view addSubview:view];
        
    }else{
        
        for (int i = 0; i<self.leftDataArray.count; i++) {
            if (i==indexPath.row) {
                UITableViewCell *cell = [self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
                cell.selected = YES;
            }else{
                UITableViewCell *cell = [self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
                cell.selected = NO;
            }
        }
        
        MOCanTypeModel *firstModel = self.leftDataArray[indexPath.row];
        self.canType = firstModel.idStr;
        self.recipeId = firstModel.recipeId;
        [self getFoodData];
    }
}

-(void)clickBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark -- 懒加载
-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44+StatusBarAndNavigationBarHeight)];
        _headerView.backgroundColor = [UIColor whiteColor];
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 25, 25)];
        [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:IMAGE(@"nav_back") forState:UIControlStateNormal];
        backButton.centerY = StatusBarHeight + (StatusBarAndNavigationBarHeight - StatusBarHeight)/2;
        [_headerView addSubview:backButton];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 32 - 100, 25)];
        titleLabel.text = @"订餐";
        titleLabel.font =  [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = KBlackColor_222222;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.center = CGPointMake(SCREEN_WIDTH/2, backButton.centerY);
        [_headerView addSubview:titleLabel];
        
        UIButton *myMoneyButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-120-16, 0, 120, 20)];
        [myMoneyButton setImage:IMAGE(@"") forState:UIControlStateNormal];
        [myMoneyButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        [myMoneyButton setTitle:@"" forState:UIControlStateNormal];
        myMoneyButton.titleLabel.font = FONT_SIZE(13);
        myMoneyButton.centerY = backButton.centerY;
        [myMoneyButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
        [_headerView addSubview:myMoneyButton];
        self.myMoneyButton = myMoneyButton;
        
        UIButton *selectedButton = [[UIButton alloc]initWithFrame:CGRectMake(15, StatusBarAndNavigationBarHeight, 91, 27)];
        [selectedButton setTitle:@"" forState:UIControlStateNormal];
        [selectedButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
        selectedButton.titleLabel.font = FONT_SIZE(14);
        [selectedButton setBackgroundColor:KGmColorWithHexColor(0xf6f6f6)];
        selectedButton.layer.masksToBounds = YES;
        selectedButton.layer.cornerRadius = 4.0;
        [selectedButton setImage:IMAGE(@"dingCanPull") forState:UIControlStateNormal];
        [selectedButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [selectedButton addTarget:self action:@selector(clickSelectedButton) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:selectedButton];
        self.selectedButton = selectedButton;
        
        UIButton *dateButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-100, StatusBarAndNavigationBarHeight, 100, 27)];
        [dateButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        dateButton.titleLabel.font = FONT_SIZE(14);
        self.selectedDate = [NSString getCurrentTimesAndFormat:@"YYYY-MM-dd"];
        [dateButton setTitle:self.selectedDate forState:UIControlStateNormal];
        [dateButton  addTarget:self action:@selector(dateButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:dateButton];
        self.dateButton = dateButton;
    }
    return _headerView;
}

-(UITableView *)leftTableView
{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _headerView.bottom+1, 77, SCREEN_HEIGHT-_headerView.height-_bottomView.height) style:UITableViewStyleGrouped];
        _leftTableView.backgroundColor = [UIColor whiteColor];
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.tag = 100;
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
    }
    return _leftTableView;
}

-(UITableView *)rightTableView
{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(_leftTableView.right+7, _headerView.bottom, SCREEN_WIDTH-77-14, SCREEN_HEIGHT-_headerView.height-_bottomView.height) style:UITableViewStyleGrouped];
        _rightTableView.backgroundColor = KGrayColor_f8f8f8;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTableView.tag = 200;
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
    }
    return _rightTableView;
}

//字符串改颜色
-(NSMutableAttributedString *)contentString:(NSString *)contentStr valueStr:(NSString *)valueStr;
{
    NSString *subStr = [NSString stringWithFormat:@"%@元",valueStr];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    NSMutableAttributedString *setString = [[NSMutableAttributedString alloc] initWithString:contentStr];
    [setString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentStr length])];
    
    NSRange range = NSMakeRange([contentStr rangeOfString:subStr].location, [contentStr rangeOfString:subStr].length);
    [setString  addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
    
    NSRange range1 = NSMakeRange([contentStr rangeOfString:valueStr].location, [contentStr rangeOfString:valueStr].length);
    [setString  addAttribute:NSForegroundColorAttributeName value:KRedColor_E71020 range:range1];
    
    return setString;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 58, SCREEN_WIDTH, 58)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(28, 0, 80, 58)];
        priceLabel.textColor = KBlackColor_2A2A2A;
        priceLabel.font = [UIFont boldSystemFontOfSize:13];
//        priceLabel.text = @"总额：0元";
        [_bottomView addSubview:priceLabel];
        self.allPriceLabel = priceLabel;
        self.allPriceLabel.attributedText = [self contentString:[NSString stringWithFormat:@"总额：0元"] valueStr:@"0"];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 8 - 179, 7, 179, 44)];
        [submitButton setBackgroundColor:KBlueColor_2C94FF];
        submitButton.layer.masksToBounds = YES;
        submitButton.layer.cornerRadius = 3.0;
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton addTarget:self action:@selector(clickSubMitButton) forControlEvents:UIControlEventTouchUpInside];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [_bottomView addSubview:submitButton];
    }
    return _bottomView;
}

@end
